package com.oceansoft.mobile.biz.api.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.common.Constant;
import com.oceansoft.mobile.common.base.entity.Result;
import com.oceansoft.mobile.common.util.HttpClientUtil;
import com.oceansoft.mobile.common.util.StringUtils;
import com.oceansoft.mobile.weixin.util.RequestUtils;
import com.oceansoft.mobile.weixin.util.WeixinUtil;


@Controller
@RequestMapping("api/findGoods")
public class ApiFindGoodsController {
	static final String FindGoodUrl = Constant.host5+"api/SWZL/GetInfo";
	@RequestMapping("getPage/{pageName}")
	public String getPage(@PathVariable("pageName") String pageName,HttpServletRequest request,Model model){
		String init = request.getParameter("init");
		if(StringUtils.isBlank(init))
		{
			request.getSession().removeAttribute("jswp");
		}
		String source = (String) request.getSession().getAttribute("source");
	       if (!StringUtils.isEmpty(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/jswpzl/"+pageName;
	}
	
	@ResponseBody
	@RequestMapping(value="getlist")
	public Result FindGoods(HttpServletRequest request,@RequestParam("name")String name ,
			@RequestParam("index") int index ,HttpSession session) throws IllegalArgumentException, IOException{
		WeixinUtil.gesearchCountInfo("13","失物招领");//统计方法调用
		Map<String, String> param = new HashMap<String, String>();
		param = RequestUtils.buildParamsByNamesFromRequest(request);
		request.getSession().setAttribute("jswp", param);
		
		String a=HttpClientUtil.doGet(FindGoodUrl,"name",name,"field","","index",index,"size",5);
		JSONObject json=JSONObject.parseObject(a);
		Result re=new Result(Result.SUCC);
		re.setData(json.get("data"));
		//将查询列表的数据存在session中，供详细页面使用
		//后续页面继续增加在session中
		if(index>1){
			JSONObject data = (JSONObject) json.get("data");
			JSONArray ja1 = data.getJSONArray("swzldata");
			JSONObject jsono = (JSONObject) session.getAttribute(Constant.SESSION_FINDGOODS);
			JSONArray ja2 = jsono.getJSONArray("swzldata");
			for(int i = 0;i<ja1.size();i++){
				ja2.add(ja1.get(i));
			}
			data.put("swzldata", ja2);
			session.setAttribute(Constant.SESSION_FINDGOODS, json.get("data"));
		}else{
			session.setAttribute(Constant.SESSION_FINDGOODS, json.get("data"));
		}
		return 	re;
	}
	
	@RequestMapping(value = "jswp_detail")
	public ModelAndView viewNews(@RequestParam("pkid")String pkid,HttpSession session,Model model,HttpServletRequest request) {
		JSONObject json = (JSONObject) session.getAttribute(Constant.SESSION_FINDGOODS);
		JSONArray ja = json.getJSONArray("swzldata");
		for(int i=0;i<ja.size();i++){
			JSONObject object = (JSONObject) ja.get(i);
			if(pkid.equals((String) object.get("pkid"))){
				model.addAttribute("goods", object);
				break;
			}
		}
		model.addAttribute("pkid", pkid);
		String source = (String) request.getSession().getAttribute("source");
	       if (!StringUtils.isEmpty(source)) {
	       	model.addAttribute("source", source);
	       }
		return new ModelAndView("weixin/jswpzl/jswp_detail");
	}

}
